﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThinhTien.Models;
using DevExpress.Web.Mvc;

using DevExpress.Data;
using DevExpress.Data.Filtering;
using DevExpress.Data.Linq;
using DevExpress.Data.Linq.Helpers;
using ThinhTien.EF4;
using ThinhTien.Models.Order;

namespace ThinhTien.Data.Repository
{
    public class OrderRepository : BaseRepository<OrderRepository>
    {
        public void AddNew(UserViewModel model)
        {
            try
            {
                using (var db = new THINHTIENDBEntities())
                {


                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(UserViewModel model)
        {
            try
            {
                using (var db = new THINHTIENDBEntities())
                {

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(List<Guid> ids)
        {
            try
            {
                using (var db = new THINHTIENDBEntities())
                {

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    public static class OrderBindingHandler
    {
        public static void GetDataRowCount(GridViewCustomBindingGetDataRowCountArgs e)
        {
            using (var db = new THINHTIENDBEntities())
            {
                e.DataRowCount = (from order in db.Orders
                                  select order).ApplyFilter(e.FilterExpression).Count();
            }
        }

        public static void GetData(GridViewCustomBindingGetDataArgs e)
        {
            using (var db = new THINHTIENDBEntities())
            {
                var query = (from order in db.Orders
                             join contact in db.Contacts on order.ContactID equals contact.ID
                             join detail in db.OrderDetails on order.ID equals detail.OrderID
                             join product in db.Products on detail.ProductID equals product.ID
                             select new
                             {
                                 order.Name,
                                 contact.Description,
                                 product.Price,
                                 detail.ID

                             }).Distinct()
                            .ApplySorting(e.State.SortedColumns)
                            .ApplyFilter(e.State.FilterExpression);


                List<UserViewModel> data = new List<UserViewModel>();
                foreach (dynamic item in query)
                {
                    
                }

                if (data.Count == 0)
                {
                    data.Add(new UserViewModel()
                    {
                        ID = Guid.NewGuid()
                    });
                }

                e.Data = data;
            }
        }
    }
}
